package com.instagram.instadownloader.bookmark;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.instagram.instadownloader.history.HistoryObject;
import com.instagram.instadownloader.utils.sharepreferences.SharePreferencesUtil;

public class BookmarksManager {
	public static String KEY_STORE_BOOKMARK = "bookmark key store";

	private BookmarksManager() {
		retrieveData();
	}

	private static BookmarksManager mInstance;

	public static BookmarksManager getInstance() {
		if (mInstance == null) {
			mInstance = new BookmarksManager();
		}
		return mInstance;
	}

	// ====================================//
	private ArrayList<BookmarkObject> mBookmarksDatas;

	public ArrayList<BookmarkObject> getBookmarks() {
		return mBookmarksDatas;
	}
	
	public BookmarkObject getBookmarkByPosition(int pos) {
		return ((pos >= mBookmarksDatas.size()) || pos == -1) ? null : mBookmarksDatas.get(pos);
	}
	
	public void addBookmark(BookmarkObject object) {
		boolean isExist = false;
		String url = object.getUrl();
		for (int i = 0; i < mBookmarksDatas.size(); i++) {
			if(url.equals(mBookmarksDatas.get(i).getUrl())){
				isExist = true;
				break;
			}
		}
		if(!isExist){
			mBookmarksDatas.add(object);
			saveData();
		}
	}
	
	public void updatePath(String url, String path) {
		for (int i = 0; i < mBookmarksDatas.size(); i++) {
			BookmarkObject obj = mBookmarksDatas.get(i);
			if(obj.getUrl().equalsIgnoreCase(url)){
				obj.setAbsolutePath(path);
				mBookmarksDatas.set(i, obj);
			}
		}
	}
	
	
	public void updateImageUrl(String url, String imageUrl) {
		for (int i = 0; i < mBookmarksDatas.size(); i++) {
			BookmarkObject obj = mBookmarksDatas.get(i);
			if(obj.getUrl().equalsIgnoreCase(url)){
				obj.setImageUrl(imageUrl);
				mBookmarksDatas.set(i, obj);
			}
		}
	}

	public void removeBookmark(BookmarkObject object) {
		if (mBookmarksDatas.contains(object)) {
			mBookmarksDatas.remove(object);
			saveData();
		}
	}


	public void removeBookmark(int pos) {
		if (mBookmarksDatas.size() > pos) {
			mBookmarksDatas.remove(pos);
			saveData();
		}
	}

	/**
	 * Save data.
	 */
	private void saveData() {
		Gson gson = new Gson();
		if (mBookmarksDatas != null) {
			String json = gson.toJson(mBookmarksDatas);
			clearData();
			SharePreferencesUtil.setStringValue(KEY_STORE_BOOKMARK, json);
		}
	}

	/**
	 * Clear data.
	 */
	private void clearData() {
		SharePreferencesUtil.removeElement(KEY_STORE_BOOKMARK);
	}

	/**
	 * Retrieve data.
	 */
	private void retrieveData() {
		Gson gson = new Gson();
		String json = SharePreferencesUtil.getStringValue(KEY_STORE_BOOKMARK, "");
		Type type = new TypeToken<List<BookmarkObject>>() {
		}.getType();
		mBookmarksDatas = gson.fromJson(json, type);

		if (mBookmarksDatas == null) {
			mBookmarksDatas = new ArrayList<BookmarkObject>();
		}
	}

}
